using System;
using System.Windows;
using Autodesk.Revit.UI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using CodRevit.Models;
using CodRevit.Services;
using CodRevit.ViewModels;
using CodRevit.Views;

namespace CodRevit.DockPanes
{
    public class CozeBotDockablePane : IDockablePaneProvider
    {
        private CozeBotDockablePaneView _paneView;
        public static readonly DockablePaneId PaneId = new DockablePaneId(new Guid("B8F58C7A-FC64-4C3F-9964-03D7E1FF3E57"));

        public static void Register(UIControlledApplication application)
        {
            try
            {
                // 注册可停靠窗格
                var dpid = PaneId;
                if (!DockablePane.PaneIsRegistered(dpid))
                {
                    application.RegisterDockablePane(dpid, "CozeBot", new CozeBotDockablePane());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"注册 DockablePane 失败: {ex.Message}");
            }
        }

        public static void Show(UIApplication uiapp)
        {
            try
            {
                var dpid = PaneId;
                DockablePane dp = uiapp.GetDockablePane(dpid);
                dp?.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"显示 DockablePane 失败: {ex.Message}");
            }
        }

        public static void Hide(UIApplication uiapp)
        {
            try
            {
                var dpid = PaneId;
                DockablePane dp = uiapp.GetDockablePane(dpid);
                dp?.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"隐藏 DockablePane 失败: {ex.Message}");
            }
        }

        public void SetupDockablePane(DockablePaneProviderData data)
        {
            try
            {
                var settings = LoadSettings();
                var apiService = new CozeApiService(settings);
                var viewModel = new CozeBotViewModel(apiService);

                _paneView = new CozeBotDockablePaneView
                {
                    DataContext = viewModel
                };
                data.FrameworkElement = _paneView;
                data.InitialState = new DockablePaneState
                {
                    DockPosition = DockPosition.Tabbed
                };
                //设置为叠加状态时，一定要设置叠加到哪个窗口上;此处设置为项目浏览器
                data.InitialState.TabBehind = DockablePanes.BuiltInDockablePanes.ProjectBrowser;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"设置 DockablePane 失败: {ex.Message}");
            }
        }

        private CozeApiSettings LoadSettings()
        {
            try
            {
                string jsonPath = Path.Combine(Path.GetDirectoryName(typeof(CozeBotDockablePane).Assembly.Location), "CozeApiSettings.json");
                string jsonContent = File.ReadAllText(jsonPath);
                var settingsObject = JsonConvert.DeserializeObject<JObject>(jsonContent);
                var settings = settingsObject["CozeApiSettings"];

                return new CozeApiSettings
                {
                    ApiKey = settings["ApiKey"].ToString(),
                    ApiUrl = settings["ApiUrl"].ToString(),
                    BotId = settings["BotId"].ToString()
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载配置文件失败: {ex.Message}");
                return null;
            }
        }
    }
}